<?php
/*
 * Session variables:
 * $_SESSION['database'] [string] = database name of the current tutorial centre
 * $_SESSION['type'] [string] = user type of the logged in user
 * $_SESSION['authorised'] [boolean] = whether or not the current user is logged in
 * $_SESSION['tcname'] [string] = the name of the tutorial centre
 * $_SESSION['user'] [string] = the username/ email of the logged in user
 */
session_start();
if(isset($_SESSION['authorised']) && $_SESSION['authorised'] == true)
{
	if(isset($_SESSION['type']))
	{
		if($_SESSION['type'] == 'administrator')
		{
			header('Location: admin_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'student')
		{
			header('Location: student_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'tutor')
		{
			header('Location: tutor_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'parent')
		{
			header('Location: parent_index.php');
			exit();
		}
	}
}
else
{
	if(isset($_POST['tcname']))
	{
		$host = 'localhost';
		$dbuser = 'postgres';
		$dbpass = '1190';
		$dbname = 'tbms_databases';
		include('connect.inc');

		$tutorial_centre = strtoupper(pg_escape_string($_POST['tcname']));
		$sql = "SELECT * FROM databases WHERE tutorial_centre = '$tutorial_centre';";
		$result = pg_query($sql);
		$count = pg_num_rows($result);

		if($count >= 1)
		{
			$_SESSION['signupmessage'] = 'Tutorial centre already exists.';
			header('Location: signup.php');
		}
		else
		{
			if($_POST['tcname'] == null || $_POST['tcname'] == "")
			{
				$_SESSION['signupmessage'] = 'Please enter the tutorial centre name';
				header('Location: signup.php');
				exit();
			}
			else
			{
				$_SESSION['tcname'] =  $tutorial_centre;
				?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sign Up</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<style>
.nav {
	width: 210px;
	height: auto;
	border: thin solid #09C;
	margin-left: 10px;
	text-align: center;
}

.mainline {
	width: 712px;
	height: auto;
	text-align: center;
}
</style>
<script type="text/javascript">
		function checkPasswordMatch() {
			var pass1 = document.getElementById('owner_password');
			var pass2 = document.getElementById('owner_password2');
			var message = document.getElementById('passwordmessage');

			if(pass1.value == pass2.value) {
				pass2.style.backgroundColor = "#66cc66";
				message.style.color = "#66cc66";
				message.innerHTML = "Passwords Match!";
			} else {
				pass2.style.backgroundColor = "#ff6666";
				message.style.color = "#ff6666";
				message.innerHTML = "Passwords Do Not Match!";
			}
		}
		</script>
	<script>
	function setOptions(chosen) 
	{
		var selbox = document.myform.birthday;
		selbox.options.length = 0;
		
		if (chosen == "Jan" || chosen == "Mar" || chosen == "May" || chosen == "Jul" || chosen == "Aug" || chosen == "Oct" || chosen == "Dec")
		{
		  <?php 
		  for($i=1; $i < 32; $i++)
		  {
		  	print ("selbox.options[selbox.options.length] = new Option($i,$i);\n");
		  }
		  ?>
		}
		else if (chosen == "Feb")
		{
		  <?php 
		  for($i=1; $i < 29; $i++)
		  {
		  	print ("selbox.options[selbox.options.length] = new Option($i,$i);\n");
		  }
		  ?>
		}
		else
		{
		  <?php 
		  for($i=1; $i < 31; $i++)
		  {
		  	print ("selbox.options[selbox.options.length] = new Option($i,$i);\n");
		  }
		  ?>
		}
		
		
	}
			</script>
</head>
<body bgcolor="#FFFFFF">
	<center>
		<div id="wrapper">
			<div id="header">
				<h1></h1>
			</div>

			<div id="header2">
				<h3></h3>
			</div>

			<div id="nav">
			<?php include('signnav.php');?>

			</div>

			<div id="mainline">
				<h4>Sign Up - Step 2 of 2</h4>
				<div id="body">
					<form name ="myform" action="emailcode.php" method="post">
						<table>
							<tr>
								<th style="color: #069" align="left">Tutorial Centre Owner
									Information</th>
							</tr>
							<tr height="15"></tr>
						</table>
						<table width="600"">
							<tr height="40">
								<th align="left">First Name:</th>
								<td><input type="text" size="50" maxlength="100" id="owner_first_name"
									name="owner_first_name" /></td>
							</tr>
							<tr height="40">
								<th align="left">Middle Name:</th>
								<td><input type="text" size="50" maxlength="100" id="owner_middle_name"
									name="owner_middle_name" /></td>
							</tr>
							<tr height="40">
								<th align="left">Last Name:</th>
								<td><input type="text" size="50" maxlength="100" id="owner_last_name"
									name="owner_last_name" /></td>
							</tr>
							<tr height="40">
								<th align="left">Email:</th>
								<td><input type="text" size="50" maxlength="100" id="owner_email" name="owner_email" /></td>
							</tr>
							<tr height="40">
								<th align="left">Password:</th>
								<td><input type="password" size="30" maxlength="30" id="owner_password"
									name="owner_password" /></td>
							</tr>
							<tr height="40">
								<th align="left">Repeat Password:</th>
								<td><input type="password" size="30" maxlength="30" id="owner_password2"
									name="owner_password2"
									onkeyup="checkPasswordMatch(); return false;" /></td>
							</tr>
							<tr height="40">
								<th align="left">Birthday:</th>
								<td><select name="birthmonth" onchange="setOptions(document.myform.birthmonth.options[document.myform.birthmonth.selectedIndex].value);">
									<option value="" selected="selected">MONTH</option>
									<option value="Jan">January</option>
									<option value="Feb">Febuary</option>
									<option value="Mar">March</option>
									<option value="Apr">April</option>
									<option value="May">May</option>
									<option value="Jun">June</option>
									<option value="Jul">July</option>
									<option value="Aug">August</option>
									<option value="Sep">September</option>
									<option value="Oct">October</option>
									<option value="Nov">November</option>
									<option value="Dec">December</option>
								</select>
								<select name="birthday">
									<option value="" selected="selected">DAY</option>
									<?php 
										for($i = 1; $i <= 31; $i++) {
											echo "<option value=$i>$i</option>";	
										}
									?>
								</select>
								<select name="birthyear">
									<?php
										for($i=1930; $i<2001; $i++)
										{
											echo "<option value=$i>$i</option>";
										}
									?>
								</select>
								</td>
							</tr>
                            <tr height="40">
								<th align="left">Gender:</th>
								<td>
									<select name ="gender">
										<option value="MALE">Male</option>
										<option value="FEMALE">Female</option>
								</td>
							</tr>
                            <tr height="40">
								<th align="left">Contact Number:</th>
								<td><input type="text" size="50" maxlength="100" id="owner_contact" name="owner_contact"/></td>
							</tr>
                            <tr height="40">
								<th align="left">Street Address 1:</th>
								<td><input type="text" size="50" maxlength="100" id="owner_street_add1" name="owner_street_add1"/></td>
							</tr>
                            <tr height="40">
                                <th align="left">Street Address 2:</th>
                                <td><input type="text" size="50" maxlength="100" id="owner_street_add2" name="owner_street_add2"/></td>
                            </tr>
                            <tr height="40">
								<th align="left">City Address:</th>
								<td><input type="text" size="50" maxlength="100" id="owner_city_add" name="owner_city_add"/></td>
							</tr>
							<tr height="40">
								<td align="left">
                                    <form action="signup.php" method="post">
                                    <input type="submit" id="back" name="back" value="Back"/>
                                    </form>
									<?php 
										echo "<input type=\"hidden\" id=\"tcname\" name=\"tcname\" value =\"$tutorial_centre\" />";
									?>
                                    <input type="submit" id="submit" name="submit" value ="Next"/></td>
							</tr>
						</table>
					</form>
				</div>
			</div>

			<p align="justify"><br /></p>
			<br class="clearfloat" />
			<br />

			<div id="footer">
				<p align="center">
					Tutorial Business Manager: info@tbms.com <br />Credits/Engineers: Aquino, Chloebelle; Dela Cruz, Roy Erwin; Maliwanag, Ranyel Bryan; Manzano, Audrey Ann; Ng, Sheila Regine
					<br /><br />
					copyright 2011
				</p>
			</div>
		</div>
		</center>
	</body>
    

<?php
            }
        }

    }
    else
    {
        header('Location: signup.php');
        exit();
    }
}
?>	
